package com.zhongya.example.fragment

import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.zhongya.example.R
import com.zhongya.example.activity.MainActivity
import com.zhongya.example.adapter.ExampleAdapter
import com.zhongya.example.bean.ExampleBean
import com.zhongya.example.databinding.ExampleFragmentMainBinding
import com.zhongya666.library.base.fragment.BaseDbFragment
import com.zhongya666.library.bean.ToExampleBean
import com.zhongya666.library.ext.initClose
import com.zhongya666.library.view.nav
import com.zhongya666.library.view.navigateAction

class MainFragment : BaseDbFragment<ExampleFragmentMainBinding>(R.layout.example_fragment_main) {

    private var toExampleBean : ToExampleBean? = null

    override fun initView() {

        toExampleBean = (activity as MainActivity).toExampleBean

        mDatabind.include.toolbar.initClose("目录") {
            activity?.finish() //路由过来的才显示返回键,直接结束
        }
        if (toExampleBean == null){
            mDatabind.include.toolbar.navigationIcon = null //如果为null 说明是单模块运行的,自然不需要返回键
        }

        val list = arrayListOf<ExampleBean>()
        val bean1 = ExampleBean("案例教程", "里面有项目中比较常用的控件")
        val bean2 = ExampleBean("自定义控件", "《自定义控件》两本书的案例练习")
        val bean3 = ExampleBean("进阶之光", "《案例练习")
        list.add(bean1)
        list.add(bean2)
        list.add(bean3)
        val adapter = ExampleAdapter(list)
        mDatabind.rv.adapter = adapter
        mDatabind.rv.layoutManager =
            StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)

        adapter.setOnItemClickListener { adapter, view, position ->
            when (position) {
                0 -> {
                    nav().navigateAction(R.id.action_mainFragment_to_demoFragment)
                }
                1 -> {
                    nav().navigateAction(R.id.action_mainFragment_to_myViewFragment)
                }
                2 -> {
                    nav().navigateAction(R.id.action_mainFragment_to_advancedFragment)
                }
            }
        }
    }

    override fun createObserver() {

    }

    override fun lazyLoadData() {

    }
}